\insert 'List.oz'
\insert 'Visualizer.oz'

%%
%% Task 2
%%

declare State ApplyMoves Moves

fun {ApplyMoves Start Moves}
   case Moves of Move|Rest then
      case Move of trackA(N) then
	 Start|{ApplyMoves {TrackA Start N} Rest}
      [] trackB(N) then
	 Start|{ApplyMoves {TrackB Start N} Rest}
      end
   else
      Start|nil
   end
end

fun {TrackB State N}
   local Main TrackA TrackB MainLength in
      if N>0 then
	 MainLength = {Length State.main} - N
	 Main = {Take State.main MainLength }
	 TrackB = {Append {Drop State.main MainLength} State.trackB}
	 TrackA = State.trackA
      else
	 TrackA = State.trackA
	 TrackB = {Drop State.trackB ~N}
	 Main = {Append State.main {Take State.trackB ~N}}
      end
      state(main:  Main trackA: TrackA trackB: TrackB)
   end
end

fun {TrackA State N}
   local Main TrackA TrackB MainLength in
      if N>0 then
	 MainLength = {Length State.main} - N
	 Main = {Take State.main MainLength }
	 TrackA = {Append {Drop State.main MainLength} State.trackA}
	 TrackB = State.trackB
      else
	 TrackB = State.trackB
	 TrackA = {Drop State.trackA ~N}
	 Main = {Append State.main {Take State.trackA ~N}}
      end
      state(main:  Main trackA: TrackA trackB: TrackB)
   end
end


%State = state(main: [a b] trackA: [c d] trackB:[e f])
%Moves = [trackA(2) trackA(~2) trackB(10)]
%{Browse {ApplyMoves State Moves}}

%{Visualize [state(main: [a b] trackA: nil trackB: nil)
%	    state(main: [a] trackA: [b] trackB: nil)
%	    state(main: [nil] trackA: [b] trackB: [a])
%	    state(main: [b] trackA: nil trackB: [a])]
%}
